﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Collections.Generic;
using Library;

namespace BusinessLayer
{
   public class Menu
   {
       #region "Variables and Properties"
        private int menu_ID;
        private int parent_Menu_ID;
        private string menu_Name;
        private string menu_Detail;
        private int menu_Status;
        private int menu_Order;



        public int Menu_Order
        {
            get { return menu_Order; }
            set { menu_Order = value; }
        }
        public int Menu_ID
        {
            get { return menu_ID; }
            set { menu_ID = value; }
        }
        public int Parent_Menu_ID
        {
            get { return parent_Menu_ID; }
            set { parent_Menu_ID = value; }
        }
        public string Menu_Name
        {
            get { return menu_Name; }
            set { menu_Name = value; }
        }
        public string Menu_Detail
        {
            get { return menu_Detail; }
            set { menu_Detail = value; }
        }
        public int Menu_Status
        {
            get { return menu_Status; }
            set { menu_Status = value; }
        }
       #endregion
       #region "Initial Methods"
        public Menu() { }

        public Menu(SqlDataReader sdr)
        {
            if (sdr != null && !sdr.IsClosed)
            {
                menu_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    parent_Menu_ID = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    menu_Name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    menu_Detail = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    menu_Status = sdr.GetInt32(4);
                if (!sdr.IsDBNull(5))
                    menu_Order = sdr.GetInt32(5);
            }
        }
       #endregion
       #region "Business Methods"
//Get all Menu
        public static List<Menu> GetAll()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Menu_SHOWALL";
            List<Menu> lstMenu = new List<Menu>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstMenu.Add(new Menu(sdr));
            }
            return lstMenu;
        }
//Get all Visible Menu
        public static List<Menu> GetVisible()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Menu_MainBar";
            List<Menu> lstMenu = new List<Menu>();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            while (sdr.Read())
            {
                lstMenu.Add(new Menu(sdr));
            }
            return lstMenu;
        }
//Get all Main Menu
       public static List<Menu> GetMain()
       {
           DBHelp dbh = new DBHelp();
           string cmd = "tbl_Menu_MAIN";
           List<Menu> lstMenu = new List<Menu>();
           SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
           while (sdr.Read())
           {
               lstMenu.Add(new Menu(sdr));
           }
           return lstMenu;
           }
//Get a Menu
        public static Menu GetOne(int menuID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Menu_ID", menuID);
            string cmd = "tbl_Menu_SHOWONE";
            Menu menu = new Menu();
            SqlDataReader sdr = dbh.ExecuteReader(cmd, CommandType.StoredProcedure);
            if(sdr.Read())
            {
                menu.Menu_ID = sdr.GetInt32(0);
                if (!sdr.IsDBNull(1))
                    menu.Parent_Menu_ID = sdr.GetInt32(1);
                if (!sdr.IsDBNull(2))
                    menu.Menu_Name = sdr.GetString(2);
                if (!sdr.IsDBNull(3))
                    menu.Menu_Detail = sdr.GetString(3);
                if (!sdr.IsDBNull(4))
                    menu.Menu_Status = sdr.GetInt32(4);
                if (!sdr.IsDBNull(5))
                    menu.Menu_Order = sdr.GetInt32(5);
            }
            return menu;
        }
//Get New Order
        public static int GetOrder()
        {
            DBHelp dbh = new DBHelp();
            string cmd = "tbl_Menu_ORDER";
            
            try
            {
                int numb = Convert.ToInt32(dbh.ExecuteScalar(cmd, CommandType.StoredProcedure));
                return numb;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
//Insert a Menu
        public static void Insert(Menu menu)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Parent_Menu_ID", menu.Parent_Menu_ID);
            dbh.AddParameter("@Menu_Name", menu.Menu_Name);
            dbh.AddParameter("@Menu_Detail", menu.Menu_Detail);
            dbh.AddParameter("@Menu_Order", menu.Menu_Order);
            string cmd = "tbl_Menu_Insert";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
//Update a Menu
        public static void Update(Menu menu)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Menu_ID", menu.Menu_ID);
            dbh.AddParameter("@Parent_Menu_ID", menu.Parent_Menu_ID);
            dbh.AddParameter("@Menu_Name", menu.Menu_Name);
            dbh.AddParameter("@Menu_Detail", menu.Menu_Detail);
            dbh.AddParameter("@Menu_Order", menu.Menu_Order);
            string cmd = "tbl_Menu_Update";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
//Delete a Menu
        public static void Delete(int menuID)
        {
            DBHelp dbh = new DBHelp();
            dbh.AddParameter("@Menu_ID", menuID);
            string cmd = "tbl_Menu_Delete";
            dbh.ExecuteNonQuery(cmd, CommandType.StoredProcedure);
        }
       #endregion


   }
}
